//package com.niujie.prototypemanage.utils;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.niujie.prototypemanage.dto.PartContent;
//import com.niujie.prototypemanage.dto.SecurityProperties;
//import com.niujie.prototypemanage.eum.Part;
//import com.niujie.prototypemanage.exception.CustomizeErrorCode;
//import com.niujie.prototypemanage.exception.CustomizeException;
//import org.apache.tomcat.util.codec.binary.Base64;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//
//import javax.crypto.Mac;
//import javax.servlet.http.HttpServletRequest;
//import java.nio.charset.StandardCharsets;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.StringTokenizer;
//
//@Component
//public class JwtUtil {
//
//    private static final String KEY = "00000000111111112222222233333333";
//
//    private static final String DOT = ".";
//
//    private static final Map<String, String> HEADERS = new HashMap<>(8);
//
//    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
//
//    static {
//        HEADERS.put("alg", "HS256");
//        HEADERS.put("typ", "JWT");
//    }
//
//    String generateHeaderPart() throws JsonProcessingException {
//        byte[] headerBytes = OBJECT_MAPPER.writeValueAsBytes(HEADERS);
//        String headerPart = new String(Base64.encodeBase64(headerBytes,false ,true), StandardCharsets.US_ASCII);
//        System.out.println("生成的Header部分为:{}"+headerPart);
//        return headerPart;
//    }
//
//    String generatePayloadPart(Map<String, Object> claims) throws JsonProcessingException {
//        byte[] payloadBytes = OBJECT_MAPPER.writeValueAsBytes(claims);
//        String payloadPart = new String(Base64.encodeBase64(payloadBytes,false ,true), StandardCharsets.UTF_8);
//        System.out.println("生成的Payload部分为:{}"+payloadPart);
//        return payloadPart;
//    }
//
//    String generateSignaturePart(String headerPart, String payloadPart) {
//        String content = headerPart + DOT + payloadPart;
//        Mac mac = HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_SHA_256, KEY.getBytes(StandardCharsets.UTF_8));
//        byte[] output = mac.doFinal(content.getBytes(StandardCharsets.UTF_8));
//        String signaturePart = new String(Base64.encodeBase64(output, false ,true), StandardCharsets.UTF_8);
//        return signaturePart;
//    }
//
//    public String generate(Map<String, Object> claims) throws Exception {
//        String headerPart = generateHeaderPart();
//        String payloadPart = generatePayloadPart(claims);
//        String signaturePart = generateSignaturePart(headerPart, payloadPart);
//        String jws = headerPart + DOT + payloadPart + DOT + signaturePart;
//        return jws;
//    }
//
//    public Map<Part, PartContent> parse(String jwt) throws Exception {
//        System.out.println("当前解析的JWT:" + jwt);
//        Map<Part, PartContent> result = new HashMap<>(8);
//        // 这里暂且认为所有的输入JWT的格式都是合法的
//        StringTokenizer tokenizer = new StringTokenizer(jwt, DOT);
//        String[] jwtParts = new String[3];
//        int idx = 0;
//        while (tokenizer.hasMoreElements()) {
//            jwtParts[idx] = tokenizer.nextToken();
//            idx++;
//        }
//        String headerPart = jwtParts[0];
//        PartContent headerContent = new PartContent();
//        headerContent.setRawContent(headerPart);
//        headerContent.setPart(Part.HEADER);
//        headerPart = new String(Base64.decodeBase64(headerPart), StandardCharsets.UTF_8);
//        headerContent.setPairs(OBJECT_MAPPER.readValue(headerPart, new TypeReference<Map<String, Object>>() {
//        }));
//        result.put(Part.HEADER, headerContent);
//        String payloadPart = jwtParts[1];
//        PartContent payloadContent = new PartContent();
//        payloadContent.setRawContent(payloadPart);
//        payloadContent.setPart(Part.PAYLOAD);
//        payloadPart = new String(Base64.decodeBase64(payloadPart), StandardCharsets.UTF_8);
//        payloadContent.setPairs(OBJECT_MAPPER.readValue(payloadPart, new TypeReference<Map<String, Object>>() {
//        }));
//        result.put(Part.PAYLOAD, payloadContent);
//        String signaturePart = jwtParts[2];
//        PartContent signatureContent = new PartContent();
//        signatureContent.setRawContent(signaturePart);
//        signatureContent.setPart(Part.SIGNATURE);
//        result.put(Part.SIGNATURE, signatureContent);
//        return result;
//    }
//
//}
